package com.xuan.system.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuan.common.core.domain.TableDataInfo;
import com.xuan.common.core.exception.BusinessException;
import com.xuan.system.domain.user.dto.UserQueryDTO;
import com.xuan.system.domain.user.dto.UserStatusDTO;
import com.xuan.system.domain.user.entity.User;
import com.xuan.system.domain.user.vo.UserVo;
import com.xuan.system.manager.UserCacheManager;
import com.xuan.system.service.user.UserService;
import com.xuan.system.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author ASUS
 * @description 针对表【tb_user(用户表)】的数据库操作Service实现
 * @createDate 2024-11-20 21:48:59
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserCacheManager userCacheManager;

    @Override
    public TableDataInfo listUser(UserQueryDTO userQueryDTO) {
        Long userId = userQueryDTO.getUserId();
        String nickName = userQueryDTO.getNickName();
        Integer pageNum = userQueryDTO.getPageNum();
        Integer pageSize = userQueryDTO.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectList(new QueryWrapper<User>()
                .select("user_id", "nick_name", "sex", "head_image", "phone", "school_name", "major_name", "introduce", "status")
                .eq(userId != null && userId > 0, "user_id", userId)
                .like(StringUtils.isNotBlank(nickName), "nick_name", nickName)
                .orderBy(true, false, "create_time"));
        if (CollectionUtils.isEmpty(userList)) {
            return TableDataInfo.empty();
        }
        List<UserVo> userVoList = BeanUtil.copyToList(userList, UserVo.class);
        return TableDataInfo.success(userVoList, new PageInfo(userVoList).getTotal());
    }

    @Override
    public int updateUserStatus(UserStatusDTO userStatusDto) {
        Long userId = userStatusDto.getUserId();
        Integer status = userStatusDto.getStatus();
        User user = getUser(userId);
        if (status != 0 && status != 1) {
            throw new BusinessException("更新信息用户状态失败");
        }
        if (Objects.equals(user.getStatus(), status)) {
            return 1;
        }
        user.setStatus(status);
        // 刷新用户详情缓存(若用户详情存在 redis 中)
        userCacheManager.updateUserStatus(userId, user.getStatus());
        return userMapper.updateById(user);
    }


    private User getUser(Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在,拉黑失败");
        }
        return user;
    }
}




